package net.coding.bamboo.wex.web;

import com.weihui.mq.constant.DeliveryMode;
import com.weihui.mq.constant.DestinationType;
import com.weihui.mq.constant.MessageFormat;
import com.weihui.mq.core.MQException;
import com.weihui.mq.core.MQService;
import com.weihui.mq.request.notify.DefaultNotifyRequest;
import net.coding.bamboo.wex.mq.model.InputMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author : zenghua
 * @version : v0.1 MqController.java
 * @since : 2017/7/14 11:23
 */
@Controller
@RequestMapping("/wex/mq")
public class MqController {
    public static final String QUEUE_DESTINATION = "mq.queue.foo";

    @Autowired
    private MQService mqService;

    @RequestMapping(value = "/rest", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String rest() {
        return "rest";
    }

    @RequestMapping(value = "/send/{id}", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String sendMessage(@PathVariable String id) {
        InputMessage inputMessage = new InputMessage();
        inputMessage.setId(Integer.parseInt(id));
        inputMessage.setCreateTime(new Date());

        DefaultNotifyRequest<InputMessage> defaultNotifyRequest = new DefaultNotifyRequest<>();
        defaultNotifyRequest.setDestination(QUEUE_DESTINATION);
        defaultNotifyRequest.setContent(inputMessage);
        defaultNotifyRequest.setTransacted(true);
        defaultNotifyRequest.setDeliveryMode(DeliveryMode.PERSISTENT);
        defaultNotifyRequest.setMessageFormat(MessageFormat.OBJECT);
        defaultNotifyRequest.setDestinationType(DestinationType.QUEUE);
        try {
            mqService.sendMessage(defaultNotifyRequest);
        } catch (MQException e) {
            e.printStackTrace();
        }
        return id;
    }
}
